package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Description:网关统一配置跨域
 * @author : xyq
 * @date: 2020/5/28/18:19
 */
@Configuration
public class GulimallCorsConfiguration {

    //  加入容器中
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();  //  跨域相关配置
        //  1、配置跨域
        corsConfiguration.addAllowedHeader("*");    //  允许所有请求头
        corsConfiguration.addAllowedMethod("*");    //  允许所有请求方式，get、post、put、detete
        corsConfiguration.addAllowedOrigin("*");    //  允许任意请求来源跨域
        corsConfiguration.setAllowCredentials(true);    //  是否允许携带cookie跨域

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

}
